package net.yuanmomo.socket.bio;

import net.yuanmomo.socket.Constant;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by Hongbin.Yuan on 2016-07-02 16:48.
 */

public class TimeServerBIO {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        try {
            // 创建一个 server socket, 接收请求
            serverSocket = new ServerSocket(Constant.PORT);

            System.out.println(String.format("Server is start on PORT %s", Constant.PORT));
            // server 接收不同的 socket 连接
            while (true) {
                Socket socket = serverSocket.accept();
                System.out.println(String.format(" receive a new socket "));
                // 新启动一个线程处理新的 socket 连接
                new Thread(new TimeServerHandlerBIO(socket)).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (serverSocket != null) {
                System.out.println(String.format("server close"));
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

